$(function () {
    $('#client_fake_datagrid').datagrid({
        fit:true,
        url:'/client/selectByStatus.do?status=0',
        pagination:true,
        //fitColumns:true,
        toolbar:"#client_fake_toolbar",
        singleSelect:true,
        rownumbers:true,
        columns:[[
            {field:'id',title:'编号',width:50,hidden:true},
            {field:'inputTime',title:'录入时间',width:100},
            {field:'inputUser',title:'录入人',width:100,formatter:function (value, row, index) {
                return row.inputUser.username;
            }},
            {field:'takeUser',title:'负责人',width:100,formatter:function (value, row, index) {
                if(row.takeUser != null){
                    return row.takeUser.username;
                }else{
                    return "<font style='color: red'>暂无负责人</font>";
                }
            }},
            {field:'name',title:'客户名称',width:100},
            {field:'age',title:'年龄',width:50},
            {field:'sex',title:'性别',width:50,formatter:function (value, row, index) {
                if(row.sex){
                    return "男";
                }else{
                    return "女";
                }
            }},
            {field:'job',title:'职位',width:50},
            {field:'salary',title:'收入',width:50},
            {field:'tel',title:'电话',width:50},
            {field:'email',title:'邮箱',width:100},
            {field:'address',title:'地址',width:50},
            {field:'source',title:'客源',width:50},
            {field:'qq',title:'QQ',width:50},
            {field:'id_card',title:'身份证',width:100},
            {field:'status',title:'状态',width:100,formatter:function (value, row, index) {
                if(row.status == 0){
                    return "潜在客户";
                }else if(row.status == 1){
                    return "正式客户";
                }else if(row.status == 3){
                    return "资源池客户";
                }
            }}
        ]]
    });

    $("#client_fake_dialog").dialog({
        height:450,
        width:300,
        buttons:"#client_fake_button",
        closed:true
    });

    $("#client_move_dialog").dialog({
        height:350,
        width:300,
        buttons:"#client_move_button",
        closed:true
    });

    $("#client_fail_dialog").dialog({
        height:300,
        width:350,
        buttons:"#client_fail_button",
        closed:true
    });

    $("#client_lose_dialog").dialog({
        height:300,
        width:350,
        buttons:"#client_lose_button",
        closed:true
    });
});

//保存移交数据
function move_save() {
    $("#client_move_form").form('submit', {
        url: '/client/updateTakeUser.do',
        success:function (data) {
            var data = $.parseJSON(data);
            if(data.success){
                $("#client_move_dialog").dialog('close');
                $.messager.alert("温馨提示",data.msg,'info',function () {
                    $("#client_fake_datagrid").datagrid('reload');
                });
            }else{
                $.messager.alert("温馨提示",data.msg,'error');
            }
        }
    })
};

//移交客户
function move() {
    var row = $("#client_fake_datagrid").datagrid('getSelected');
    if(!row){
        $.messager.alert("温馨提示","麻烦您选中一条数据！",'error');
        return;
    }
    $("#client_move_dialog").dialog('setTitle','移交潜在用户');
    $("#client_move_dialog").dialog('open');

    $("#client_move_form").form('load',row);
}


function add() {
    $("#client_fake_dialog").dialog('setTitle','新增潜在用户');
    $("#client_fake_form").form('clear');
    $("#client_fake_dialog").dialog('open');
}

function edit() {
    var row = $("#client_fake_datagrid").datagrid('getSelected');
    if(!row){
        $.messager.alert("温馨提示","麻烦您选中一条数据！",'error');
        return;
    }
    $("#client_fake_dialog").dialog('setTitle','编辑潜在用户');
    $("#client_fake_dialog").dialog('open');

    $('#client_fake_dialog').form('load',row);
}

function save() {
    $("#client_fake_form").form('submit', {
        url: '/client/saveOrUpdate.do',
        onSubmit:function (param) {
            param["status"] = 0;
        },
        success: function (data) {
            var data = $.parseJSON(data);
            if(data.success){
                $("#client_fake_dialog").dialog('close');
                $.messager.alert("温馨提示",data.msg,'info',function () {
                    $("#client_fake_datagrid").datagrid('reload');
                });
            }else{
                $.messager.alert("温馨提示",data.msg,'error');
            }
        }
    });
}

function pool() {
    var row = $("#client_fake_datagrid").datagrid('getSelected');
    if(!row){
        $.messager.alert("温馨提示","麻烦您选中一条数据！",'error');
        return;
    }
    //发送id修改状态，放入客户资源池
    var selected_id = row.id;
    $.messager.confirm("温馨提示","确认要将此客户移入资源池吗？",function (data) {
        if(data){
            $.get('/client/updateStatus.do',{'id':selected_id,'status':3});
            $("#client_fake_datagrid").datagrid('reload');
        }
    });
}

function query() {
    var name = $("#name").textbox('getValue');
    var id_card = $("#id_card").textbox('getValue');
    var tel = $("#tel").textbox('getValue');
    var beginTime = $("#beginTime").textbox('getValue');
    var endTime = $("#endTime").textbox('getValue');
    $("#client_fake_datagrid").datagrid('load',{
        name:name,
        id_card:id_card,
        tel:tel,
        beginTime:beginTime,
        endTime:endTime
    });
}

function open_lose() {
    var row = $("#client_fake_datagrid").datagrid('getSelected');
    if(!row){
        $.messager.alert("温馨提示","麻烦您选中一条数据！",'error');
        return;
    }
    //客户流失不需要判断takeUser
    $("#client_lose_dialog").dialog('setTitle','设置为客户流失');
    $("#client_lose_form").form('load',row);
    $("#client_lose_dialog").dialog('open');
}



function open_fail() {
    var row = $("#client_fake_datagrid").datagrid('getSelected');
    if(!row){
        $.messager.alert("温馨提示","麻烦您选中一条数据！",'error');
        return;
    }
    if(row.takeUser == null){
        $.messager.alert("温馨提示","客户都还没开发呢，怎么会失败呢？找个最厚脸皮的去跪舔嘛~",'info');
        return;
    }
    $("#client_fail_dialog").dialog('setTitle','设置为开发失败');
    $("#client_fail_form").form('load',row);
    $("#client_fail_dialog").dialog('open');
}

//客户开发失败
function fail() {
    $("#client_fail_form").form('submit',{
        url: '/client/updateStatus.do',
        onSubmit:function (param) {
            param["status"]=4;
        },
        success:function (data) {
            var data = $.parseJSON(data);
            if(data.success){
                $("#client_fail_dialog").dialog('close');
                $.messager.alert("温馨提示",data.msg,'info',function () {
                    $("#client_fake_datagrid").datagrid('reload');
                });
            }else{
                $.messager.alert("温馨提示",data.msg,'error');
            }
        }
    })
};

//客户流失
function lose() {
    $("#client_lose_form").form('submit',{
        url: '/client/updateStatus.do',
        onSubmit:function (param) {
            param["status"]=5;
        },
        success:function (data) {
            var data = $.parseJSON(data);
            if(data.success){
                $("#client_lose_dialog").dialog('close');
                $.messager.alert("温馨提示",data.msg,'info',function () {
                    $("#client_fake_datagrid").datagrid('reload');
                });
            }else{
                $.messager.alert("温馨提示",data.msg,'error');
            }
        }
    })
}

